//package com.batata.continuing.web.demo;
//
//import com.alibaba.fastjson.JSONObject;
//import com.batata.continuing.config.moreNote.token.NotNeedToken;
//import com.rabbitmq.client.Channel;
//import com.rabbitmq.client.MessageProperties;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.AmqpException;
//import org.springframework.amqp.core.*;
//
//import org.springframework.amqp.rabbit.connection.ConnectionFactory;
//import org.springframework.amqp.rabbit.connection.CorrelationData;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//
//
///**
// * 测试mq使用
// */
//@Slf4j
//@RestController
//@RequestMapping("/mq")
//public class MqController {
//
//
//    //rabbitMq
//    @Resource
//    private AmqpTemplate amqpTemplate;
//
//    @Resource
//    private RabbitTemplate rabbitTemplate;
//
//    //点对点
//    @NotNeedToken
//    @PostMapping("simpleSender")
//    @ApiOperation(value = "简单队列发送消息", notes = "简单队列发送消息")
//    public void simpleSender(String content) {
//        Map<String, String> map = new HashMap<>();
//        map.put("content", content);
//        map.put("key", "aaaa");
//        log.info("简单队列发送消息:{}", map);
//        //amqpTemplate.convertAndSend("simple", JSONObject.toJSONString(map));
//
//        CorrelationData correlationData = new CorrelationData("12345678909" + new Date());
//
//        rabbitTemplate.convertAndSend("simple", (Object) JSONObject.toJSONString(map), new MessagePostProcessor() {
//            @Override
//            public Message postProcessMessage(Message message) throws AmqpException {
//                //设置消息持久化
//                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
//                //message.getMessageProperties().setHeader("x-delay", "6000");
//                message.getMessageProperties().setDelay(6000);
//                return message;
//            }
//        }, correlationData);
//    }
//
//    //work
//    @ApiOperation(value = "work模式发送消息", notes = "work模式发送消息")
//    @PostMapping("workSender")
//    public void workSender(String content) {
//        log.info("work模式发送消息:{}", content);
//        for (int i = 1; i <= 10; i++) {
//            amqpTemplate.convertAndSend("work", content + i);
//        }
//    }
//
//    //广播
//    @ApiOperation(value = "广播模式发送消息", notes = "广播模式发送消息")
//    @PostMapping("fanoutSender")
//    public void fanoutSender(String content) {
//        log.info("广播模式发送消息:{}", content);
//        amqpTemplate.convertAndSend("fanout.test", "", content);
//    }
//
//    //路由
//    @ApiOperation(value = "路由模式发送消息", notes = "路由模式发送消息")
//    @PostMapping("directSender")
//    public void directSender(String content, String key, String exchange) {
//        log.info("路由模式发送消息:{},key:{},exchange{}", content, key, exchange);
//        amqpTemplate.convertAndSend(exchange, key, content);
//    }
//
//    //点对点
//    @ApiOperation(value = "延时队列发送", notes = "延时队列发送")
//    @PostMapping("delayQueue")
//    public void delayQueue(String content) {
//        try {
//            ConnectionFactory connectionFactory = rabbitTemplate.getConnectionFactory();
//            Channel channel = connectionFactory.createConnection().createChannel(false);
//            String realQueue = "real.queue";
//            String dlxQueue = "dlx.queue";
//            String realExchang = "real.exchang";
//            String dlxExchang = "dlx.exchang";
//            String dlxKey = "dlx.#";
//            Map<String, Object> map = new HashMap<>();
//            map.put("x-dead-letter-exchange", dlxExchang);
//            map.put("x-message-ttl", 10000);
//            channel.exchangeDeclare(realExchang, "direct", true, false, null);
//            channel.queueDeclare(realQueue, true, false, false, map);
//            //channel.queueBind(realQueue,realExchang,dlxKey);
//            channel.queueBind(realQueue, realExchang, "#");
//            String mess = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "创建";
//
//            //创建死信交换机和队列
//            channel.exchangeDeclare(dlxExchang, "direct", true, false, null);
//            channel.queueDeclare(dlxQueue, true, false, false, null);
//            //channel.queueBind(dlxQueue,dlxExchang,"#");
//            channel.queueBind(dlxQueue, dlxExchang, "#");
//
//            //channel.basicPublish(realExchang,"dlx.aa",
//            // MessageProperties.PERSISTENT_TEXT_PLAIN,mess.getBytes(StandardCharsets.UTF_8));
//            channel.basicPublish(realExchang, "#",
//                    MessageProperties.PERSISTENT_TEXT_PLAIN, mess.getBytes(StandardCharsets.UTF_8));
//            System.err.println("消息发送完成......");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//
//}
